package com.example.express;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.example.express.base.BaseActivity;
import com.example.express.entity.User;
import com.example.express.utlis.MD5Utils;
import com.example.express.utlis.ToastUtil;

import org.litepal.LitePal;

public class RegisterActivity extends BaseActivity {

    private static Boolean bool;
    public static void start(Context context,Boolean bool){
        RegisterActivity.bool=bool;
        context.startActivity(new Intent(context,RegisterActivity.class));
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_register;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        TextView mTitleTv = findViewById(R.id.m_title_tv);
        TextView mTvCommit = findViewById(R.id.tvCommit);
        findViewById(R.id.m_back_iv).setOnClickListener(v -> finish());
        mTvCommit.setOnClickListener(v->register());
        if (bool){
            mTitleTv.setText("重置密码");
            mTvCommit.setText("立即重置");
        }else {
            mTitleTv.setText("注册");
        }
    }

    private void register() {
        EditText etPhone = findViewById(R.id.etPhone);
        EditText etPassword = findViewById(R.id.etPassword);
        EditText etConfirmPassword = findViewById(R.id.etConfirmPassword);
        EditText etNickName = findViewById(R.id.etNickName);
        String phone = etPhone.getText().toString().trim();
        String password = etPassword.getText().toString().trim();
        String confirmPassword = etConfirmPassword.getText().toString().trim();
        String nickname = etNickName.getText().toString().trim();

        if (TextUtils.isEmpty(phone)) {
            ToastUtil.show("请输入账号");
            return;
        }

        if (TextUtils.isEmpty(password)) {
            ToastUtil.show("请输入密码");
            return;
        }

        if (TextUtils.isEmpty(confirmPassword)) {
            ToastUtil.show("请再次输入密码");
            return;
        }

        if (!TextUtils.equals(password, confirmPassword)) {
            ToastUtil.show("两次输入的密码不一致");
            return;
        }

        if (TextUtils.isEmpty(nickname)) {
            ToastUtil.show("请输入昵称");
            return;
        }

        password = MD5Utils.digest(password);
        User user = new User(phone, password, nickname);

        if(bool){
            User user1 = LitePal.where("phone =? and nickname =?", phone, nickname).findFirst(User.class);
            if (user1==null) {
                ToastUtil.show("账号验证无效");
                return;
            }else {
                user.update(user1.getId());
                ToastUtil.show("重置密码成功");
                finish();
            }
        }else {
            boolean save = user.save();
            if (!save) {
                ToastUtil.show("账号已存在");
                return;
            }else {
                ToastUtil.show("注册成功");
                finish();
            }
        }
    }
}